import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:share_plus/share_plus.dart';

import '../../../event/DataCallBack.dart';
import '../../../nets/NetWorkManager.dart';
import '../../../utils/tips.dart';

class videoUtil {
  static String _shareUrl = '';

  static void setShareUrl(url) {
    _shareUrl = url;
  }

  static shareInvite(bool isVoice) async {
    final typeText = isVoice?'语音':'视频';
    final result = await SharePlus.instance.share(
      ShareParams(text: '${_shareUrl}你的好友邀请您${typeText}通话'),
    );
    return result;
  }

  //   请求相关
  static transcription(
    int appId,
    int roomId,
    int peerId,
    user,
    String srcCode,
    String tgtCode,
  ) async {
    print('srcCode${srcCode}-tgtCode${tgtCode}');
    BaseOptions options = BaseOptions();
    options.headers["contentType"] = "multipart/form-data";
    options.headers["X-Api-Key"] = '264e03da4958e5ea405dd8f63b87da898757b821';
    try {
      Dio dio = new Dio(options);
      var response = await dio.post(
        "https://xy-apis.newtranx.com/chat/v1/room/start-transcription",
        data: {
          'user': user.toString(),
          'room': roomId.toString(),
          'peerId': peerId.toString(),
          'target': tgtCode,
          'source': srcCode,
          // 'source':'zh-CN',
          // 'target':'en-US'
        },
      );
      print('check123${response}');
      // 处理服务器返回的响应数据
      if (response.statusCode == 200) {
        print('checkinvite1data${response.data}');
        final res = response.data['data'];
        return res;
      } else {
        Tips.showToast('${response}');
        return false;
      }
    } catch (error) {
      print('chatcherror ${error}');
      Tips.showToast('${error}');
      return false;
    }
  }

  static rtcAuth(String srcCode,String tgtCode,isVoice) async {
    BaseOptions options = BaseOptions();
    options.headers["contentType"] = "multipart/form-data";
    options.headers["X-Api-Key"] = '264e03da4958e5ea405dd8f63b87da898757b821';
    try {
      Dio dio = new Dio(options);
      var response = await dio.get(
        "https://xy-apis.newtranx.com/chat/v1/room/inviter?voiceCall=${isVoice}",
      );

      // 处理服务器返回的响应数据
      if (response.statusCode == 200) {
        print('checkinvitedata${response.data}');
        // await transcription(
        //   response.data['data']['appId'],
        //   response.data['data']['room'],
        //   response.data['data']['peerId'],
        //   response.data['data']['user'],
        //   srcCode,
        //   tgtCode,
        // );
        return response.data['data'];
      } else {
        Tips.showToast('${response}');
        // print('checkinviteres${response}');
        return null;
      }
    } catch (error) {
      Tips.showToast('${error}');
      // print('checkinviteerror${error}');
      return null;
    }
  }

  static void showHandupDialog(BuildContext context, callback) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return CupertinoAlertDialog(
          title: Container(
            margin: EdgeInsets
                .fromLTRB(
              0, 10, 0, 0,),
            child: Text('温馨提示', style: TextStyle(fontSize: 20)),
          ),
          content: Container(
            margin: EdgeInsets
                .fromLTRB(
              0, 10, 0, 10,),
            child: Text(
              '确认要结束当前通话吗？',
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 14, color: Color((0xFF333333))),
            ),
          ),
          actions: <CupertinoDialogAction>[
             CupertinoDialogAction(
               child: Text('取消',style: TextStyle(
                   color:Color(0xFF0D65E6)),),
               onPressed: () {
                 Navigator.of(context).pop(); // 关闭对话框并返回null（不设置返回值）
               },
             ),
             CupertinoDialogAction(
               child: Text('确认',style: TextStyle(
        color:Color(0xFF0D65E6)),),
               onPressed: () async {
                 Navigator.of(context).pop();
                 callback();
               },
             ),


          ],
        );
      },
    );
  }

  static showFontsizeDialog(BuildContext context, currentFontsize, callback) {
    String? value; // 用于存储选中的值
    value = currentFontsize.toString();
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('调整字体大小'),
          content: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return Row(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.min, // 使对话框尽可能小
                children: <Widget>[
                  Row(
                    children: [
                      Radio(
                        value: '14',
                        groupValue: value,
                        onChanged: (String? newValue) {
                          setState(() {
                            // 更新选中的值
                            value = newValue;
                            Future.delayed(Duration(milliseconds: 400), () {
                              Navigator.of(context).pop(newValue);
                            });
                          });
                        },
                      ),
                      SizedBox(width: 4.0), // 添加间距
                      Text('14px'),
                    ],
                  ),
                  Row(
                    children: [
                      Radio(
                        value: '16',
                        groupValue: value,
                        onChanged: (String? newValue) {
                          setState(() {
                            // 更新选中的值
                            value = newValue;
                            Future.delayed(Duration(milliseconds: 400), () {
                              Navigator.of(context).pop(newValue);
                            });
                          });
                        },
                      ),
                      // SizedBox(height: 8.0), // 添加间距
                      Text('16px'),
                    ],
                  ),
                  Row(
                    children: [
                      Radio(
                        value: '18',
                        groupValue: value,
                        onChanged: (String? newValue) {
                          setState(() {
                            // 更新选中的值
                            value = newValue;
                            Future.delayed(Duration(milliseconds: 400), () {
                              Navigator.of(context).pop(newValue);
                            });
                          });
                        },
                      ),
                      // SizedBox(height: 8.0), // 添加间距
                      Text('18px'),
                    ],
                  ),
                ],
              );
            },
          ),
        );
      },
    ).then((value) {
      if ((value != null) && (value != currentFontsize.toString())) {
        print('checkdialogcalue ${value}');
        callback(value);
        return value;
      }
    });
  }
}
